package com.itcast.donkey.adapters;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.JourneyActivity;
import com.itcast.donkey.activity.ScenicAttackActivity;
import com.itcast.donkey.activity.TopicActivity;
import com.itcast.donkey.activity.TravelAreaActivity;
import com.itcast.donkey.modles.ScenicSpotModles;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * 攻略下的第二层页面的适配器
 * <p/>
 * Created by lanouhn on 16/8/25.
 */
public class ScenicAdapter extends RecyclerView.Adapter<ScenicAdapter.ScenicViewHoder> {

    private Context context;
    private ArrayList<ScenicSpotModles> spotModlesArrayList;

    public ScenicAdapter(Context context, ArrayList<ScenicSpotModles> spotModlesArrayList) {
        this.context = context;
        this.spotModlesArrayList = spotModlesArrayList;
    }

    @Override
    public ScenicViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_scenic_spot, null);
        ScenicViewHoder holder = new ScenicViewHoder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ScenicViewHoder holder, final int position) {
        Picasso.with(context).load(spotModlesArrayList.get(position).getImage_url()).into(holder.ivItemSpotBackground);
        holder.ivItemSpotBackground.setScaleType(ImageView.ScaleType.FIT_XY);

        holder.tvItemSpotTitle.setText(spotModlesArrayList.get(position).getName_zh_cn());
        holder.tvItemSpotEnglish.setText(spotModlesArrayList.get(position).getName_en());

        //点击下载按钮,弹出通知栏
        holder.ivItemSpotDownload.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {

                NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(context);
                builder.setTicker("正在下载");
                builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_chanyouji_logo));
                builder.setContentTitle("蝉游记");
                builder.setContentText("正在下载");
                builder.setSmallIcon(R.mipmap.ic_launcher);

                nm.notify(10, builder.build());
            }
        });

        //进入行程
        holder.ivItemSpotJourney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, JourneyActivity.class);
                intent.putExtra("id", spotModlesArrayList.get(position).getId());
                intent.putExtra("name", spotModlesArrayList.get(position).getName_zh_cn());
                context.startActivity(intent);
            }
        });

        //进入攻略
        holder.ivItemSpotAttack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ScenicAttackActivity.class);
                intent.putExtra("Wiki_app_publish", spotModlesArrayList.get(position).isWiki_app_publish());
                intent.putExtra("id", spotModlesArrayList.get(position).getId());
                intent.putExtra("name", spotModlesArrayList.get(position).getName_zh_cn());
                context.startActivity(intent);
            }
        });

        //进入旅行地
        holder.ivItemSpotTravel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TravelAreaActivity.class);
                intent.putExtra("id", spotModlesArrayList.get(position).getId());
                intent.putExtra("name", spotModlesArrayList.get(position).getName_zh_cn());
                context.startActivity(intent);
            }
        });

        //进入专题
        holder.ivItemSpotTopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TopicActivity.class);
                intent.putExtra("id", spotModlesArrayList.get(position).getId());
                intent.putExtra("name", spotModlesArrayList.get(position).getName_zh_cn());
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return spotModlesArrayList.size();
    }


    public class ScenicViewHoder extends RecyclerView.ViewHolder {

        private ImageView ivItemSpotBackground;
        private TextView tvItemSpotTitle;
        private TextView tvItemSpotEnglish;
        private ImageView ivItemSpotDownload;
        private ImageView ivItemSpotAttack;
        private ImageView ivItemSpotJourney;
        private ImageView ivItemSpotTravel;
        private ImageView ivItemSpotTopic;

        public ScenicViewHoder(View itemView) {
            super(itemView);
            ivItemSpotBackground = (ImageView) itemView.findViewById(R.id.iv_item_spot_background);
            tvItemSpotTitle = (TextView) itemView.findViewById(R.id.tv_item_spot_title);
            tvItemSpotEnglish = (TextView) itemView.findViewById(R.id.tv_item_spot_english);

            ivItemSpotDownload = (ImageView) itemView.findViewById(R.id.iv_item_spot_download);

            ivItemSpotAttack = (ImageView) itemView.findViewById(R.id.iv_item_spot_attack);
            ivItemSpotJourney = (ImageView) itemView.findViewById(R.id.iv_item_spot_journey);
            ivItemSpotTravel = (ImageView) itemView.findViewById(R.id.iv_item_spot_travel);
            ivItemSpotTopic = (ImageView) itemView.findViewById(R.id.iv_item_spot_topic);
        }
    }
}
